package com.demo.inboundhandlerandoutboundhandler.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author gy
 * @date 2022/7/31
 */
public class MyServerHandler extends SimpleChannelInboundHandler<Long> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Long msg) throws Exception {
        ChannelPipeline pipeline = ctx.pipeline();
        System.out.println("从客户端" + ctx.channel().remoteAddress() + "读取到long " + msg);

        // 服务器在收到客户端的消息后，做出响应，同样发送一个long类型的数据给客户端
        System.out.println("服务器在收到客户端的消息后，做出响应，同样发送一个long类型的数据给客户端");
        ctx.writeAndFlush(98765L);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
